package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.ab.view.titlebar.AbTitleBar;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.MrqgdDjList;
import com.htandroid.htandroid.sys.Basenvo;

import java.util.ArrayList;

/**
 * Created by lkp on 2015/12/4.
 */
public class mrqgd_djdetail extends AbActivity {
    private int icurrentrow;
    private ArrayList<MrqgdDjList> mrqgdlist;
    private MrqgdDjList mrqgds;

    private AbTitleBar mAbTitleBar = null;
    private View rightView1 = null;
    private View rightView2 = null;
    private View rightView3 = null;
    private TextView tv_currentrow;

    @AbIocView(id = R.id.iquantity_tv)
    TextView iquantity_tv;
    @AbIocView(id = R.id.btn1_tv, click = "btnClick")
    TextView btn1_tv;
    @AbIocView(id = R.id.cven_tv)
    EditText cven_tv;
    @AbIocView(id = R.id.iprice_et)
    EditText iprice_et;
    @AbIocView(id = R.id.iamount_tv)
    EditText iamount_tv;
    @AbIocView(id = R.id.cdepname_tv)
    TextView cdepname_tv;
    @AbIocView(id = R.id.cinvname_tv)
    TextView cinvname_tv;
    @AbIocView(id = R.id.cinvunit_tv)
    TextView cinvunit_tv;
    @AbIocView(id = R.id.cinvstd_tv)
    TextView cinvstd_tv;
    @AbIocView(id = R.id.cmemo_tv)
    TextView cmemo_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_mrqgd_djdetail);
        initview();
        insert_data(icurrentrow);
    }

    public void btnClick(View v) {
        updatedataref();
        //返回上级界面
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("mrqgds", mrqgds);
        intent.putExtras(bundle);
        intent.putExtra("icurrentrow", icurrentrow);
        setResult(R.layout.layout_mrqgd_djdetail/100000, intent);  //设置返回结果
        finish();
    }

    //初始化
    private void initview() {
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("厨房定价");
        mAbTitleBar.setLogo(R.drawable.icon_back);
        mAbTitleBar.getLogoView().setLayoutParams(new LinearLayout.LayoutParams(58, 57));
        mAbTitleBar.getLogoView().setFocusable(true);
        mAbTitleBar.getLogoView().setFocusableInTouchMode(true);
        mAbTitleBar.getLogoView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("currentrow", icurrentrow);//把当前行返回到上一页面,方便更换上一页面的选中行
                setResult(R.layout.basenvo_nofunc/300000, intent);  //设置返回结果
                //返回时增加提示,防止误点
                finish();
            }
        });
        mAbTitleBar.setTitleBarBackground(R.color.common_title);
        mAbTitleBar.setTitlePadding(118, 0, 0, 0);

        mAbTitleBar.clearRightView();
        rightView1 = mInflater.inflate(R.layout.btn_abs_back, null);
        mAbTitleBar.addRightView(rightView1);
        rightView2 = mInflater.inflate(R.layout.tv_currentrow, null);
        mAbTitleBar.addRightView(rightView2);
        rightView3 = mInflater.inflate(R.layout.btn_abs_forward, null);
        mAbTitleBar.addRightView(rightView3);
        tv_currentrow = (TextView) rightView2.findViewById(R.id.tv_currentrow);

        //获取传递的对象参数
        mrqgdlist = (ArrayList<MrqgdDjList>) getIntent().getSerializableExtra("mrqgdlist");
        icurrentrow = getIntent().getExtras().getInt("icurrentrow");

        //单价修改的监听
        iprice_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    String iquantity = iquantity_tv.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
                return false;
            }
        });

        //单价焦点变化
        iprice_et.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                } else {
                    String iquantity = iquantity_tv.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
            }
        });
    }

    //为窗口填充数据
    public void insert_data(int row) {
        mrqgds = mrqgdlist.get(row);
        //填充数据
        icurrentrow = row;
        tv_currentrow.setText((icurrentrow + 1) + "/" + (mrqgdlist.size()));

        //不可修改部分
        cdepname_tv.setText(mrqgds.getCdepname());
        cinvname_tv.setText(mrqgds.getcInvname());
        cinvunit_tv.setText(mrqgds.getcInvm_unit());
        cinvstd_tv.setText(mrqgds.getcInvstd());
        cmemo_tv.setText(mrqgds.getCmemo());
        cven_tv.setText(mrqgds.getCvenname());
        cven_tv.setTag(mrqgds.getCvencode());
        iquantity_tv.setText(Basenvo.customFormat("#####0.00##", mrqgds.getiQuantity()));
        //可修改部分
        iprice_et.setText(Basenvo.customFormat("#####0.00##", mrqgds.getIprice()));
        iamount_tv.setText(Basenvo.customFormat("#####0.00##", mrqgds.getIamount()));
    }

    //供应商选择
    public void cven_choose(View v) {
        String btntype = v.getTag().toString();
        String cvencode = "";
        cvencode = cven_tv.getTag().toString();
        Intent intent = new Intent(getApplicationContext(), Basenvo_Choosecven.class);
        // 设置传递数据
        intent.putExtra("btntype", btntype);
        intent.putExtra("ccode", cvencode);
        startActivityForResult(intent, R.layout.basenvo_rchoose/200000);
    }

    //对一些修改后的值进行处理,避免因为没触发事件导致保存的信息不正确
    public void updatedataref() {
        String iquantity = iquantity_tv.getText().toString();
        String iprice = iprice_et.getText().toString();
        if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
            iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
        } else {
            iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
        }
        if (cven_tv.getTag() != null) {
            mrqgds.setCvencode(cven_tv.getTag().toString());
            mrqgds.setCvenname(cven_tv.getText().toString());
        }
        if (iprice_et.getText() != null) {
            mrqgds.setIprice(iprice_et.getText().toString());
        }
        if (iamount_tv.getText() != null) {
            mrqgds.setIamount(iamount_tv.getText().toString());
        }
    }

    //接收返回数据(供应商和存货明细)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case R.layout.basenvo_rchoose/200000:
                if (data != null) {
                    //刷新list
                    Bundle bundle = data.getExtras();
                    String cvencode = bundle.getString("cvencode");
                    String cvenname = bundle.getString("cvenname");
                    String cvenemail = bundle.getString("cvenemail");
                    //供应商则必须从列表中获取.
                    cven_tv.setText(cvenname);
                    cven_tv.setTag(cvencode);
                    mrqgds.setCvencode(cvencode);
                    mrqgds.setCvenname(cvenname);
                    mrqgds.setCvenemail(cvenemail);
                }
                break;
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    //向左翻页
    public void btn_abs_back(View v) {
        updatedataref();
        if (icurrentrow == 0) {
            AbToastUtil.showToast(getApplicationContext(), "已经是第一项");
        } else {
            insert_data(icurrentrow - 1);
        }
    }

    //向右翻页
    public void btn_abs_forward(View v) {
        updatedataref();
        if (icurrentrow == (mrqgdlist.size() - 1)) {
            AbToastUtil.showToast(getApplicationContext(), "已经是最后一项");
        } else {
            insert_data(icurrentrow + 1);
        }
    }
}
